package org.dev.hi.hiui.amount

import android.content.Context
import android.os.Build
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView

/**
 * Author WenPing
 * CreateTime 2021/9/4.
 * Description:购买数量的自定义view
 */
class AmountView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null,
    defStyle: Int = 0
) : LinearLayout(context, attributeSet, defStyle) {

    /**
     * 购买数量发生变化的回调
     */
    private lateinit var amountValueChangedCallback: (Int) -> Unit

    private val amountViewAttrs = AttrsParse.parseAmountViewAttrs(
        context,
        attributeSet,
        defStyle
    )
    private var amountValue = amountViewAttrs.amountValue

    init {
        orientation = LinearLayout.HORIZONTAL
        gravity = Gravity.CENTER
        applyAttrs()
    }

    private fun applyAttrs() {
        val increaseBtn = generateBtn()
        increaseBtn.text = "+"

        val decreaseBtn = generateBtn()
        decreaseBtn.text = "-"

        val amountView = generateAmountTextView()
        amountView.text = amountValue.toString()

        // 分别添加按钮和购买数量的控件
        addView(decreaseBtn)
        addView(amountView)
        addView(increaseBtn)

        // 设置可点击的情况
        decreaseBtn.isEnabled = amountValue > amountViewAttrs.amountMinValue
        increaseBtn.isEnabled = amountValue < amountViewAttrs.amountMaxValue

        decreaseBtn.setOnClickListener {
            amountValue--
            amountView.text = amountValue.toString()
            decreaseBtn.isEnabled = amountValue > amountViewAttrs.amountMinValue
            increaseBtn.isEnabled = true
            // 传递购买数量,调用回调
            amountValueChangedCallback(amountValue)
        }
        increaseBtn.setOnClickListener {
            amountValue++
            amountView.text = amountValue.toString()
            increaseBtn.isEnabled = amountValue < amountViewAttrs.amountMaxValue
            decreaseBtn.isEnabled = true
            amountValueChangedCallback(amountValue)
        }

    }

    private fun generateAmountTextView(): TextView {
        val view = TextView(context)
        view.apply {
            setPadding(0, 0, 0, 0)
            setTextColor(amountViewAttrs.amountTextColor)
            setBackgroundColor(amountViewAttrs.amountBackground)
            gravity = Gravity.CENTER
            includeFontPadding = false

            val params = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
            params.leftMargin = amountViewAttrs.margin
            params.rightMargin = amountViewAttrs.margin
            layoutParams = params
            minWidth = amountViewAttrs.amountSize
        }
        return view
    }

    private fun generateBtn(): Button {
        val button = Button(context)
        button.setBackgroundResource(0)
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            button.elevation = 0f
        }
        button.setPadding(0, 0, 0, 0)
        button.apply {
            includeFontPadding = false
            setTextColor(amountViewAttrs.btnTextColor)
            setTextSize(TypedValue.COMPLEX_UNIT_PX, amountViewAttrs.btnTextSize)
            setBackgroundColor(amountViewAttrs.btnBackground)
            gravity = Gravity.CENTER

            button.layoutParams = LayoutParams(
                amountViewAttrs.btnSize,
                amountViewAttrs.btnSize
            )
        }
        return button
    }

    fun getAmountValue(): Int {
        return amountValue
    }

    /**
     * 设置监听
     */
    fun setAmountValueChangedListener(amountValueChangedCallback: (Int) -> Unit) {
        this.amountValueChangedCallback = amountValueChangedCallback
    }


}